package bigbrother.webportal;

import bigbrother.BigBrotherWebPortal;
import bigbrother.core.IrcBotCore;
import bigbrother.modules.Module;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

import java.util.List;

/**
 * A webpage that contains components provided by different modules.
 *
 * @author Hans Haggstrom
 */
public class WebPortal
        extends WebPage
{

    //======================================================================
    // Private Constants

    private static final long serialVersionUID = 1L;

    //======================================================================
    // Public Methods

    //----------------------------------------------------------------------
    // Constructors

    /**
     * Constructor that is invoked when page is invoked without a session.
     *
     * @param parameters Page parameters
     */
    public WebPortal( final PageParameters parameters )
    {
        final IrcBotCore ircContext = BigBrotherWebPortal.getIrcContext();
        add( new Label( "title", ircContext.getBotModelAndVersion() ) );

        final List<Module> moduleList = ircContext.getActiveModules();

        final ListView listView = new ListView( "modules", moduleList )
        {

            public void populateItem( final ListItem listItem )
            {
                final Module module = (Module) listItem.getModelObject();

                listItem.add( new Label( "moduleName", module.getIdentifier() ) );

                final Component webComponent = module.createWebComponent( "moduleContent" );
                if ( webComponent != null )
                {
                    listItem.add( webComponent );
                }
                else
                {
                    listItem.add( new Label( "moduleContent", "" ) );
                }
            }

        };
        add( listView );


    }

    //----------------------------------------------------------------------
    // Static Methods


}

